//
//  WikiDepartmentViewController.m
//  安好
//
//  Created by rlp on 14-12-5.
//  Copyright (c) 2014年 lianchuangbrother. All rights reserved.
//

#import "WikiDepartmentViewController.h"

#import "RegHospitalListViewController.h"
#import "SelfInspectionViewController.h"
//百科医院过来的请求
#import "DoctorDetailViewController.h"
//分类挂号--科室
#import "ClassifyRegViewController.h"
@interface WikiDepartmentViewController ()
/**
 *  父级科室
 */
@property(nonatomic,copy)NSString * parentdeptid;
/**
 *  子科室
 */
@property(nonatomic,copy)NSString * departmentid;
@end

@implementation WikiDepartmentViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    self.edgesForExtendedLayout=UIRectEdgeNone;
    
    if (!self.wikiDepartmentBaseClass) {
        self.wikiDepartmentBaseClass=[[WikiDepartmentBaseClass alloc]init];
    }
    if (!self.wikiDepartmentBaseClass2) {
        self.wikiDepartmentBaseClass2=[[WikiDepartmentBaseClass alloc]init];
    }
    [self makeNavBarView];
    [self makeTableView];
    [self loadDatarReturnType1];


}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.

}
#pragma mark - 加载数据 父科室列表页
-(void)loadDatarReturnType1{
    [self showActionView];
    NSMutableDictionary *dict = [[NSMutableDictionary alloc]initWithCapacity:10];
    [dict setObject:kPlatform forKey:@"platform"];
    [dict setObject:kVersion forKey:@"version"];
    [dict setObject:@"1" forKey:@"returnType"];//1：科室列表页 2：用于弹出菜单
    [dict setObject:@"0" forKey:@"parentdeptid"];
    AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
    [manager POST:[NSString stringWithFormat:@"%@%@",kBaseURL,WIKI_DEPARTMENT] parameters:dict success:^(AFHTTPRequestOperation *operation, id responseObject) {
        MyNSLog("成功");
        NSNumber * statusNum = [responseObject objectForKey:@"status"];
        //用NSNumberFormatter把 NSNumber类型的数字-->转成字符串
        NSNumberFormatter* numberFormatter = [[NSNumberFormatter alloc] init];
        NSString * statusString = [numberFormatter stringFromNumber:statusNum];//得到字符串对象: "0" / "1"
        MyNSLog("%@",responseObject);
        if ([statusString isEqualToString:@"0"]) {
            [self showCustomHudSingleLine:[responseObject objectForKey:@"msg"]];//失败
        }else{
            if ([responseObject isKindOfClass:[NSDictionary class]]) {
                self.wikiDepartmentBaseClass = [WikiDepartmentBaseClass modelObjectWithDictionary:responseObject];
                

                
                [self.tableView reloadData];
//                [self.tableView setSeparatorStyle:UITableViewCellSeparatorStyleSingleLine];
                [self loadDatarReturnType2:0];
            }else{
                [self showCustomHudSingleLine:@"网络数据格式错误"];
            }
            
        }
        [self hiddenActionView];
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        [self hiddenActionView];
        
    }];
    
    
}
#pragma mark - 加载数据 弹出列表
-(void)loadDatarReturnType2:(NSInteger)index{
    [self showActionView];
    NSMutableDictionary *dict = [[NSMutableDictionary alloc]initWithCapacity:10];
    [dict setObject:kPlatform forKey:@"platform"];
    [dict setObject:kVersion forKey:@"version"];
    [dict setObject:@"2" forKey:@"returnType"];//1：科室列表页 2：用于弹出菜单
    [dict setObject:[(WikiDepartmentData *)[self.wikiDepartmentBaseClass.data objectAtIndex:index] departmentid] forKey:@"parentdeptid"];
    //得到父级科室
    self.parentdeptid = [(WikiDepartmentData *)[self.wikiDepartmentBaseClass.data objectAtIndex:index] parentdeptid];
    AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
    [manager POST:[NSString stringWithFormat:@"%@%@",kBaseURL,WIKI_DEPARTMENT] parameters:dict success:^(AFHTTPRequestOperation *operation, id responseObject) {
        MyNSLog("成功");
        NSNumber * statusNum = [responseObject objectForKey:@"status"];
        //用NSNumberFormatter把 NSNumber类型的数字-->转成字符串
        NSNumberFormatter* numberFormatter = [[NSNumberFormatter alloc] init];
        NSString * statusString = [numberFormatter stringFromNumber:statusNum];//得到字符串对象: "0" / "1"
        MyNSLog("%@",responseObject);
        if ([statusString isEqualToString:@"0"]) {
            [self showCustomHudSingleLine:[responseObject objectForKey:@"msg"]];//失败
        }else{
            if ([responseObject isKindOfClass:[NSDictionary class]]) {
                self.wikiDepartmentBaseClass2 = [WikiDepartmentBaseClass modelObjectWithDictionary:responseObject];
                
                [self.tableView2 reloadData];
            }else{
                [self showCustomHudSingleLine:@"网络数据格式错误"];
            }
            
        }
        [self hiddenActionView];
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        [self hiddenActionView];
        
    }];
    
    
}
/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/
#pragma mark - 设置导航条
-(void)makeNavBarView
{
    
    if([[self.navigationController.viewControllers objectAtIndex:self.navigationController.viewControllers.count-2] isKindOfClass:[ ClassifyRegViewController class]]){
    self.title = @"选择科室";
    }else
    {
        self.title = @"科室自查";
    
    }
        //贴图
    UIBarButtonItem *backBtnImg = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"公用_返回.png"] style:UIBarButtonItemStylePlain target:self action:@selector(leftButtonClicked:)];
    backBtnImg.tintColor = [UIColor whiteColor];
    self.navigationItem.leftBarButtonItem = backBtnImg;
    
 }

-(void)leftButtonClicked:(UIButton *)sender
{
    [self.navigationController popViewControllerAnimated:YES];
    

    
    
}
#pragma mark - 创建tableView 设置table的代理、添加头视图横线
-(void)makeTableView
{
    self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(0,0, kScreenWidth, kScreenHeight-iOS7TopMargin)style:UITableViewStylePlain];
    [self.view addSubview:self.tableView];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    self.tableView.backgroundColor= [ClassMethodsViewController colorWithHexString:BACK_BACKGROUNDCOLOR_16];
    //隐藏横向和竖向的滑条
    self.tableView.showsHorizontalScrollIndicator = NO;
    self.tableView.showsVerticalScrollIndicator = NO;
    [self.tableView setSeparatorStyle:UITableViewCellSeparatorStyleNone];
    //创建头视图 - 一条线
    self.headerLineView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, 0.5)];
    self.headerLineView.backgroundColor = [ClassMethodsViewController colorWithHexString:LINE_BACKGROUNDCOLOR_16];
    self.tableView.tableHeaderView = self.headerLineView;
    [self makeTableView2];
}
-(void)makeTableView2
{
    self.tableView2= [[UITableView alloc] initWithFrame:CGRectMake(self.tableView.frame.size.width,self.tableView.frame.origin.y, kScreenWidth/3*2, self.tableView.frame.size.height)];
    self.tableView2.backgroundColor = [ClassMethodsViewController colorWithHexString:BACK_BACKGROUNDCOLOR_16];
    self.tableView2.layer.backgroundColor =[ClassMethodsViewController colorWithHexString:LINE_BACKGROUNDCOLOR_16].CGColor;
    [self.view addSubview:self.tableView2];
    self.tableView2.delegate = self;
    self.tableView2.dataSource = self;
    //隐藏横向和竖向的滑条
    self.tableView2.showsHorizontalScrollIndicator = NO;
    self.tableView2.showsVerticalScrollIndicator = NO;
    [self.tableView2 setSeparatorStyle:UITableViewCellSeparatorStyleNone];
    //创建头视图 - 一条线
    self.headerLineView = [[UIImageView alloc] initWithFrame:CGRectMake(self.tableView.frame.size.width, 0, kScreenWidth/3*2, 0.5)];
    self.headerLineView.backgroundColor = [ClassMethodsViewController colorWithHexString:LINE_BACKGROUNDCOLOR_16];
    self.tableView2.tableHeaderView = self.headerLineView;
}
#pragma mark - tableView的代理方法

/**
 *  设置 各行cell高
 */
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return ((kScreenWidth == 414) ? 44*1.2 : 44);
}
/**
 *  设置 头视图的行高
 */
-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 0;
}
-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
    return 0;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    NSInteger numbers=0;
    if (self.tableView == tableView) {
        numbers= self.wikiDepartmentBaseClass.data.count;
    }else{
        if (!self.wikiDepartmentBaseClass2) {
            numbers=0;
        }else{
            numbers= self.wikiDepartmentBaseClass2.data.count;

        }

    }
    return numbers;
}
#pragma mark --cell
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *ID=@"tableViewCell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:ID];
    
    if (cell == nil) {
        cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:ID];
    }
    
    if (self.tableView == tableView) {

        cell.textLabel.font = [UIFont systemFontOfSize:LABEL_FONT_SIZE_CONTENT];
        cell.textLabel.text = [(WikiDepartmentData *)[self.wikiDepartmentBaseClass.data objectAtIndex:indexPath.row] departmentname];
        
        /**
         *  线条的处理
         */
        
        UIView *bottomLineView = [[UIView alloc]initWithFrame:CGRectMake(5, ((kScreenWidth == 414) ? 44*1.2 : 44)-0.5,kScreenWidth-5, 0.5)];

        
        bottomLineView.backgroundColor = [ClassMethodsViewController colorWithHexString:LINE_BACKGROUNDCOLOR_16];
        [cell addSubview:bottomLineView];
        
        
        cell.selectedBackgroundView = [[UIView alloc] initWithFrame:cell.frame];
        cell.selectedBackgroundView.backgroundColor = [ClassMethodsViewController colorWithHexString:LINE_BACKGROUNDCOLOR_16];
    }else{
        cell.textLabel.font = [UIFont systemFontOfSize:LABEL_FONT_SIZE_CONTENT];
        cell.textLabel.text = [(WikiDepartmentData *)[self.wikiDepartmentBaseClass2.data objectAtIndex:indexPath.row] departmentname];
        
        cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
        
        /**
         *  线条的处理
         */
        

        UIView *bottomLineView = [[UIView alloc]initWithFrame:CGRectMake(5, ((kScreenWidth == 414) ? 44*1.2 : 44)-0.5,kScreenWidth-5, 0.5)];
        
        bottomLineView.backgroundColor = [ClassMethodsViewController colorWithHexString:LINE_BACKGROUNDCOLOR_16];
        [cell addSubview:bottomLineView];
        
        cell.backgroundColor = [ClassMethodsViewController colorWithHexString:LINE_BACKGROUNDCOLOR_16];
         cell.selectionStyle =UITableViewCellSelectionStyleNone;
    }
    
    return cell;
}
#pragma mark --选中跳转
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (self.tableView == tableView) {
        [UIView animateWithDuration:0.3 animations:^{
            self.tableView.frame = CGRectMake(self.tableView.frame.origin.x, self.tableView.frame.origin.y, kScreenWidth/3+25, self.tableView.frame.size.height);

        }];
        
        [UIView animateWithDuration:0.3 animations:^{
            self.tableView2.frame = CGRectMake(self.tableView.frame.size.width, self.tableView2.frame.origin.y, kScreenWidth/3*2-25, self.tableView2.frame.size.height);
                     }];
        [self loadDatarReturnType2:indexPath.row];
        
    }else{
        //从挂号--按科室查找
        if ([self.fromFlag isEqualToString:@"reg"]) {
            WikiDepartmentData * data = (WikiDepartmentData*)[self.wikiDepartmentBaseClass2.data objectAtIndex:indexPath.row];
            self.departmentid = data.departmentid;
            RegHospitalListViewController * hospital = [[RegHospitalListViewController alloc] init];
            hospital.parDic = @{@"departmentid":self.departmentid};
            hospital.fromFlag = @"按科室";
            [self.navigationController pushViewController:hospital animated:YES];
            
        }else if([[self.navigationController.viewControllers objectAtIndex:self.navigationController.viewControllers.count-2] isKindOfClass:[ SelfInspectionViewController class]]){
            MyNSLog("%@",[self.wikiDepartmentBaseClass2.data objectAtIndex:indexPath.row]);
            SymptomDiseaseViewController *symptomDiseaseViewController = [[SymptomDiseaseViewController alloc]initWithNibName:@"SymptomDiseaseViewController" bundle:nil];
            symptomDiseaseViewController.wikiDepartmentData =(WikiDepartmentData*)[self.wikiDepartmentBaseClass2.data objectAtIndex:indexPath.row];
            symptomDiseaseViewController.title =[(WikiDepartmentData*)[self.wikiDepartmentBaseClass2.data objectAtIndex:indexPath.row] departmentname];
            [self.navigationController pushViewController:symptomDiseaseViewController animated:YES];
        }
    }


}
-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    UIView *bottomLineView = [[UIView alloc]initWithFrame:CGRectMake(0, 0,kScreenWidth, 0)];
    
    bottomLineView.backgroundColor = [ClassMethodsViewController colorWithHexString:LINE_BACKGROUNDCOLOR_16];
    return bottomLineView;
}

@end
